//
//  HomeViewModel.swift
//  DKE-Album
//
//  Created by 杨东宝 on 2020/6/15.
//  Copyright © 2020 杨东宝. All rights reserved.
//

import UIKit
import CoreBluetooth
// 委托协议
protocol DeviceListViewModelDelegate: AnyObject {
    //刷新
    func refresh()
}
class DeviceListViewModel: BaseViewModel {
    
    private let bluetoothManager = BluetoothManagerTool.shared
    weak var delegate: DeviceListViewModelDelegate?
    
    var onStateUpdate: ((ConectState) -> Void)?
    
    var bluList = Array<DeviceModel>()
    override init() {
        super.init()
        setupBluetooth()
    }
    private func setupBluetooth() {
        // 蓝牙状态回调
        bluetoothManager.onStateUpdate = { [weak self] state in
            self?.onStateUpdate?(state)
        }
        //发现设备回调
        bluetoothManager.onDeviceDiscovered = { [weak self] devices in
            self?.bluList = devices
            self?.delegate?.refresh()
        }
    }
}
/*
 *蓝牙扫描处理
 */
extension DeviceListViewModel{
    //扫描蓝牙
    func startScan(fileName:String) {
        bluetoothManager.nameFilter = fileName
        //停止扫描
        bluetoothManager.stopScanning()
        //移除数据并刷新页面
        bluList.removeAll()
        delegate?.refresh()
        // 开始扫描
        bluetoothManager.startScanning()
    }
    //扫描蓝牙
    func stopScan() {
        bluetoothManager.stopScanning()
    }
}
/*
 *wifi扫描设备的实现
 */


